package cn.dankal.flyaudio.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.CategoryListEntity;
import cn.dankal.flyaudio.R;

public class ClassificationTabAdapter extends BaseRecyclerViewAdapter<CategoryListEntity> {

    private int selectPos = 0;

    private OnTabChangeListener listener;

    public void setListener(OnTabChangeListener listener) {
        this.listener = listener;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_store_tab;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public void setSelect(String uuid) {
        for (int i = 0; i < getDatas().size(); i++) {
            if (TextUtils.equals(getDatas().get(i).getUuid(), uuid)) {
                selectPos = i;
            }
        }
        notifyDataSetChanged();
    }

    public class ViewHolder extends BaseRecyclerViewHolder<CategoryListEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(CategoryListEntity data, int position) {
            TextView classificationName = itemView.findViewById(R.id.tv_classification_name);
            classificationName.setText(data.getName());
            itemView.setSelected(position == selectPos);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPos = position;
                    if (listener != null) {
                        listener.onTabChange(data);
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }

    public interface OnTabChangeListener {

        void onTabChange(CategoryListEntity entity);
    }
}
